package checklist.main;

import checklist.db.ChecklistDAOFactory;
import checklist.entity.Check;
import checklist.entity.Checklist;
import checklist.service.UserService;
import checklist.vo.UserVO;

public class Main {
	private enum Menu {
		FAQLIST("1", "add a new faqlist"), //
		CHECK("2", "add a new check to a faqlist"), //
		SEARCHFAQLIST("3", "search for a faqlist"), //
		USER("4", "add a new user"), //
		USERCHECK("5", "add a check to a user"), //
		QUIT("q", "quit"), //
		NULL("0", "invalid menu");

		private String option;
		private String description;

		private Menu(String option, String description) {
			this.option = option;
			this.description = description;
		}

		public String getOption() {
			return option;
		}

		public String getDescription() {
			return description;
		}

		public static Menu parseMenu(String option) {
			for (Menu menu : Menu.values()) {
				if (menu != Menu.NULL && option.equals(menu.getOption())) {
					return menu;
				}
			}
			return Menu.NULL;
		}
	}

	private final static byte buffer[] = new byte[256];

	private static String formatMenu() {
		StringBuilder builder = new StringBuilder();

		for (Menu menu : Menu.values()) {
			if (menu != Menu.NULL) {
				builder.append(String.format("%s - %s\n", menu.getOption(),
						menu.getDescription()));
			}
		}
		return builder.toString();
	}

	private static String read() {
		try {
			return new String(buffer, 0, System.in.read(buffer) - 1);
		} catch (Exception e) {
			return "";
		}
	}

	private static String formatChecklist(Checklist checklist) {
		StringBuilder string = new StringBuilder();

		string.append(checklist.getId());
		for (Check check : checklist) {
			string.append(String.format("\n\t* %s\n\t%s", check.getTitle(),
					check.getDescription()));

		}

		return string.toString();
	}

	public static void main(String[] args) {
		UserService userService = new UserService();
		
		System.out.println("Welcome to checklist");

		Menu choice = null;
		String menu = formatMenu();

		while (choice != Menu.QUIT) {
			System.out.print(String.format("%s->", menu));

			choice = Menu.parseMenu(read());
			switch (choice) {

			case CHECK:
				System.out.print("Enter a checklist: ");
				String game = read();

				System.out.print("Enter a title: ");
				String title = read();

				System.out.print("Enter a description: ");
				String description = read();

				new ChecklistDAOFactory().newChecklistDAO().addCheck(game,
						title, description);
				break;

			case FAQLIST:
				System.out.print("Enter a new checklist: ");
				new ChecklistDAOFactory().newChecklistDAO()
						.addChecklist(read());
				break;

			case SEARCHFAQLIST:
				System.out.print("Enter a checklist: ");
				System.out.println(formatChecklist(new ChecklistDAOFactory()
						.newChecklistDAO().searchChecklist(read())));
				break;

			case USER:
				System.out.print("Enter a name: ");
				UserVO user = userService.createNew(read());
				System.out.println(String.format("Added user %s (id %s)", user.getName(), user.getId()));				
				break;
				
			case USERCHECK:
				System.out.print("Enter username: ");				
				break;

			case QUIT:
				System.out.println("The end");
				break;

			default:
				break;
			}
		}
	}
}
